package com.xuecheng.content.controller;

import com.xuecheng.api.content.model.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseBaseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController//=responsebody+controller
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService  courseBaseService;


    // 接口：/content/course/list
    @Override
    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params,@RequestBody QueryCourseBaseModel model) {
        Long companyId = SecurityUtil.getCompanyId();
        PageVO pageVO = courseBaseService.queryCourseBaseList(params, model,companyId);
        return pageVO;
    }

    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO vo) {

        // 1.获取公司id
        Long companyId = SecurityUtil.getCompanyId();
        // 2.vo转为dto
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(vo);
        dto.setCompanyId(companyId);

        CourseBaseDTO result = courseBaseService.createCourseBase(dto);

        return result;
    }

    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO vo) {

        // 1.获取公司id
        Long companyId = SecurityUtil.getCompanyId();

        // 2.vo转为dto
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(vo);
        dto.setCompanyId(companyId);

        CourseBaseDTO result = courseBaseService.modifyCourseBase(dto);

        return result;
    }

    @GetMapping("course/{courseId}")
    public CourseBaseDTO getCourseBaseById(@PathVariable Long courseId) {

        Long companyId = SecurityUtil.getCompanyId();

        CourseBaseDTO dto = courseBaseService.getCourseBaseById(courseId, companyId);

        return dto;
    }


    @DeleteMapping("course/{courseBaseId}")
    public void removeCourseBase(@PathVariable Long courseBaseId) {
        // 1.获得公司Id
        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.removeCourseBase(courseBaseId, companyId);
    }

    @GetMapping("course/commit/{courseId}")
    public void commitCourseBase(@PathVariable Long courseId) {
        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.commitCourseBase(courseId, companyId);
    }
}
